package com.qzct.controller;


import com.qzct.entity.MinioFile;
import com.qzct.entity.MyBucket;
import com.qzct.entity.vo.ResultVo;
import com.qzct.service.MinioFileService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.qzct.entity.vo.ResultCode;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *  控制层。
 *
 * @author 沈舜洁
 * @since 2024-12-25
 */
@RestController
@Api(tags = "test")
@RequestMapping("/minio")
public class MinioFileController {

    @Autowired
    private MinioFileService minioFileService;
    @PostMapping(value = "/upload/image",consumes = "multipart/form-data")
    public ResultVo imageUploader(@RequestPart(value = "image") MultipartFile image, @RequestParam("图片分类") String catename, @RequestParam("图片分类id") Integer cateid, @RequestParam("相册id") Integer albumid){
        String fileName=image.getOriginalFilename();
       MinioFile minioFile= minioFileService.upLoadImg("images",fileName,image,catename,cateid,albumid);
       if(minioFile==null){
           return ResultVo.error(ResultCode.FILE_UPLOAD_ERROR);
       }
       Map<String,Object> map=new HashMap<>();
       map.put("data",minioFile);

        return  new ResultVo(minioFile);
    }

    @GetMapping("/buckets")
    public  ResultVo getAllBuckets() throws Exception{

       List<MyBucket> buckets= minioFileService.findAllBuckets();

       if(buckets==null){
          return ResultVo.error(ResultCode.NO_DATA_ERROR);
       }

       return new ResultVo(buckets);
    }




}
